package com.suke.czx.modules.goods.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.GoodsSaveGroup;
import com.suke.czx.common.validate.GoodsUpdateGroup;
import com.suke.czx.modules.goods.entity.Goods;
import com.suke.czx.modules.goods.entity.GoodsSearchBo;
import com.suke.czx.modules.goods.service.GoodsService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 应用模块名称: 物品
 * 代码描述: 物品控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/03 15:28:35
 */
@Validated
@RestController
@RequestMapping("/api/goods")
@AllArgsConstructor
@Api(value = "GoodsController", tags = "物品管理")
public class GoodsController extends AbstractController {

    private final GoodsService goodsService;

    /**
     * 查询物品列表
     * @param goodsSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:goods:select')")
    public R select(GoodsSearchBo goodsSearchBo) {
        if (goodsSearchBo.getType() == null) {
            return R.error("物品类型不能为空");
        }
        // 分页查询信息列表
        return goodsService.selectGoodsList(goodsSearchBo);
    }

    /**
     * 保存物品
     * @param goods 物品
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存物品")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:goods:save')")
    public R save(@Validated(GoodsSaveGroup.class) Goods goods, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        goods.setCreator(getUserId());
        goods.setCreateTime(new Date());
        return goodsService.saveGoods(goods);
    }

    /**
     * 更新物品
     * @param goods 物品
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新物品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:goods:update')")
    public R update(@Validated(GoodsUpdateGroup.class) Goods goods, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        goods.setUpdater(getUserId());
        goods.setUpdateTime(new Date());
        return goodsService.updateGoods(goods);
    }

    /**
     * 删除物品
     * @param goodsId 物品编码
     * @return VO
     */
    @SysLog("删除物品")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:goods:delete')")
    public R delete(@NotNull(message = "物品编码不能为空") Long goodsId) {
        return goodsService.updateDelGoods(goodsId, getUserId());
    }

    /**
     * 同步项目合同清单
     * @param projectId 项目编码
     * @return VO
     */
    @SysLog("同步项目合同清单")
    @RequestMapping(value = "/sync")
    @PreAuthorize("hasRole('but:goods:sync')")
    public R sync(@NotNull(message = "项目编码不能为空") Long projectId) {
        return goodsService.sync(projectId, getUserId());
    }

    /**
     * 撤销同步项目合同清单
     * @param projectId 项目编码
     * @return VO
     */
    @SysLog("撤销同步项目合同清单")
    @RequestMapping(value = "/delSync")
    @PreAuthorize("hasRole('but:goods:delSync')")
    public R delSync(@NotNull(message = "项目编码不能为空") Long projectId) {
        return goodsService.delSync(projectId, getUserId());
    }

    /**
     * 批量交付物品
     * @param goodsList 物品数据集
     * @return VO
     */
    @SysLog("批量交付物品")
    @RequestMapping(value = "/batchDelivery")
    @PreAuthorize("hasRole('but:goods:batchDelivery')")
    public R batchDeliver(@NotNull(message = "物品数据集不能为空和") @RequestBody List<Goods> goodsList) {
        return goodsService.batchDeliver(goodsList, getUserId());
    }

}